package com.garlic.face.jlf.ui;

import android.content.Context;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.support.annotation.Nullable;

import com.garlic.face.jlf.App;
import com.garlic.face.jlf.R;

import java.util.Arrays;

import android_serialport_api.SerialPortFinder;

//管理员页面
public class SerialSettingActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener{

    private static final String TAG = "SerialSettingActivity";
    private SerialPortFinder mSerialPortFinder;
    private String[] entries;
    private String[] entryValues;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        mSerialPortFinder = App.getInstances().getSerialPortFinder();
        //getPreferenceManager().setSharedPreferencesName("SerialPort");
        // Devices
        final ListPreference eleDevices = (ListPreference) findPreference("scale_serial");
        ListPreference baudrates = (ListPreference) findPreference("scale_baudrates");
        ListPreference lightBaudrates = (ListPreference) findPreference("light_baudrates");
        ListPreference lightDevices = (ListPreference) findPreference("light_serial");
        EditTextPreference bagSmallEdit = (EditTextPreference)findPreference("bag_small");
        EditTextPreference bagMiddleEdit = (EditTextPreference)findPreference("bag_middle");
        EditTextPreference bagBigEdit = (EditTextPreference)findPreference("bag_big");
        entries = mSerialPortFinder.getAllDevices();
        entryValues = mSerialPortFinder.getAllDevicesPath();
//        DevicesName = new ArrayList<>(Arrays.asList(entries));
//        DevicesPath = new ArrayList<>(Arrays.asList(entryValues));
        System.out.println(this.getClass().getSimpleName() + "  " + entries.toString());

//        if (!lightDevices.getValue().isEmpty()) {
//            entries = Util.arraySpeDel(entries, lightDevices.getEntry().toString());
//            entryValues = Util.arraySpeDel(entryValues, lightDevices.getValue());
//        }

        eleDevices.setEntries(entries);
        eleDevices.setEntryValues(entryValues);
        eleDevices.setSummary(eleDevices.getValue());
        eleDevices.setOnPreferenceChangeListener(this);


        baudrates.setSummary(baudrates.getValue());
        baudrates.setOnPreferenceChangeListener(this);


//        if (!eleDevices.getValue().isEmpty()) {
//            entries = Util.arraySpeDel(entries, eleDevices.getEntry().toString());
//            entryValues = Util.arraySpeDel(entryValues, eleDevices.getValue());
//        }
        lightDevices.setEntries(entries);
        lightDevices.setEntryValues(entryValues);
        lightDevices.setSummary(lightDevices.getValue());
        lightDevices.setOnPreferenceChangeListener(this);
        lightBaudrates.setSummary(lightBaudrates.getValue());
        lightBaudrates.setOnPreferenceChangeListener(this);

        bagSmallEdit.setSummary(bagSmallEdit.getText());
        bagSmallEdit.setOnPreferenceChangeListener(this);
        bagMiddleEdit.setSummary(bagMiddleEdit.getText());
        bagMiddleEdit.setOnPreferenceChangeListener(this);
        bagBigEdit.setSummary(bagBigEdit.getText());
        bagBigEdit.setOnPreferenceChangeListener(this);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
//        switch (preference.getKey()) {
//            case "electronic_port":
//                entries=mSerialPortFinder.getAllDevices();
//                entryValues=mSerialPortFinder.getAllDevicesPath();
//                break;
//            case "light_port":
//                entries=mSerialPortFinder.getAllDevices();
//                entryValues=mSerialPortFinder.getAllDevicesPath();
//                break;
//        }
        preference.setSummary(newValue.toString());
        return true;
    }

    public String[] notRepetition(String[] content, String pp) {
        int len = content.length;
        String[] temp = new String[content.length - 1];
        for (int i = 0; i < content.length; i++) {
            if (content[i].equals(pp)) {
                for (int n = i; n < len; n++) {
                    content[n] = content[n + 1];
                }
                break;
            }
        }
        temp = Arrays.copyOf(content, len - 1);
        return temp;
    }
}

